package com.swn.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PropertiesUtil {
	private Properties props = new Properties();

	private String comm = "";

	private static Map<String, PropertiesUtil> propMap = null;

	public void setComm(String common) {
		this.comm = common;
	}

	public String getComm() {
		return this.comm;
	}

	public void load(InputStream input) throws IOException {
		this.props.load(new InputStreamReader(input, "UTF-8"));
	}

	public void load(InputStream input, String encode) throws IOException {
		this.props.load(new InputStreamReader(input, encode));
	}

	public Properties getProps() {
		return this.props;
	}

	public String getProperty(String key) {
		return this.props.getProperty(key);
	}

	public void setProperty(String key, String value) {
		this.props.setProperty(key, value);
	}

	public Set<Object> getKeys() {
		return this.props.keySet();
	}

	public static Properties getInstance(String filename) {
		if (propMap == null) {
			propMap = new HashMap<String, PropertiesUtil>();
		}
		if (propMap.get(filename) == null) {
			PropertiesUtil propsUtil = new PropertiesUtil();
			InputStream is = Thread.currentThread().getContextClassLoader()
					.getResourceAsStream(filename);
			try {
				propsUtil.load(is);
			} catch (IOException e) {
				e.printStackTrace();
				try {
					is.close();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			} finally {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			propMap.put(filename, propsUtil);
		}
		return ((PropertiesUtil) propMap.get(filename)).getProps();
	}

	public static String getValue(String filename, String key) {
		return getInstance(filename).getProperty(key);
	}

	public static String getValue(String filename, String key,
			String defaultValue) {
		String str = getInstance(filename).getProperty(key);
		if (str == null) {
			return defaultValue;
		}
		return str;
	}
	
	public static void main(String[] args){
		
		//Properties pu=PropertiesUtil.getInstance("frame.properties");
		String global_xss = "(((%3D)|(=))[^\n]*((%27)|(')|(--)|(%3B)|(:)|(%23)|(#)))|(\\w*((%27)|('))((%6F)|o|(%4F))((%72)|r|(%52)))|(exec((\\s)|(%20))+(s|x)p\\w+)|((<|%3c)/?(script|iframe|frame|body|img))|(set-cookie)|(src=('|\")javascript:)|(Content-\\w+:)";
		//global_xss=pu.getProperty("global.xss");
		String str="{Content-:src='javas1cript'oracle<iframe exec   ap1 =}set-1cookie";
		str = "script*";
		Pattern xssPattern = Pattern.compile(global_xss, 2);
		Matcher matcher = xssPattern.matcher(str);
		boolean isFind = matcher.find();
		if (isFind) {
			System.out.println("  中包含非法字符：" + matcher.group());
		} else
			System.out.println("  中不包含非法字符：");
	}
}